#include "common.h"
//发送给客户端的函数
void snd_to();
//创建TCP服务端成功返回sfd失败返回-1
int CreateSocket(int port);
//使用epoll来处理事件
int Epoll(user *l);
//登录判断处理
void Login(user_data *data, user *l);
//创建客户端信息链表
user *create_link();
//客户端信息链表的添加
int adduser(user_data *data, user *l);
//添加注册的用户的信息
int adddata(user_data *data, user *l);
//客户段信息查找匹配用户密码,失败返回-1
int find(user_data *data, user *l);
//普通用户权限处理事件
int common_user(user_data *data, user *l);
//管理用户权限处理事件
int manage_user(user_data *data, user *l);
//普通用户修改密码
int common_change_password(user_data *data, user *l);
//判断链表是否为空
int empty(user *l);
//查询用户
int find_user(user_data *data, user *l);
//退出系统
int exit_user(user_data *data, user *l, Set_client *setclient);
//显示所有用户
int show(user_data *data, user *l, int *cfd, Set_client *setclient);
//管理员添加用户
int manage_adduser(user_data *data, user *l);
//管理员删除用户
int manage_delete(user_data *data, user *l);
//管理员修改用户
int manage_change(user_data *data, user *l);
char find_show_buf[1024];
//数据从文件放在链表里面
int txt_to_linklist(user *l);
//链表数据传输到文件中
int linklist_to_txt(user *l);
int main(int argc, const char *argv[])
{
	user *l = NULL;
	l = create_link();
	if (l == NULL)
	{
		printf("create to user_create error\n");
		return -1;
	}
	//数据从文件放在链表里面
	txt_to_linklist(l);
	Epoll(l);

	return 0;
}
//创建客户端信息链表失败返回NULL
user *create_link()
{
	user *l = NULL;
	l = (user *)malloc(sizeof(user));
	if (l == NULL)
	{
		printf("malloc user error\n");
		return NULL;
	}
	l->next = NULL;
	return l;
}
//创建TCP服务端
int CreateSocket(int port)
{
	int sfd;
	struct sockaddr_in servicaddr;
	int alen = sizeof(servicaddr);
	sfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sfd < 0)
	{
		perror("socket");
		return -1;
	}
	servicaddr.sin_family = AF_INET;
	servicaddr.sin_port = htons(port);
	servicaddr.sin_addr.s_addr = 0;
	int ret;
	ret = bind(sfd, (struct sockaddr *)&servicaddr, alen);
	if (ret < 0)
	{
		perror("bind");
		return -1;
	}
	ret = listen(sfd, 5);
	if (ret < 0)
	{
		perror("listen");
		return -1;
	}
	return sfd;
}
//使用epoll来处理事件成功返回0失败返回-1
int Epoll(user *l)
{
	int epfd, sfd, ret, len, cfd, i, alen, slen;
	int fd;
	user_data *data = NULL;
	data = (user_data *)malloc(sizeof(user_data));
	if (data == NULL)
	{
		perror("user_data error\n");
		return -1;
	}
	Set_client *setclient = NULL;
	setclient = (Set_client *)malloc(sizeof(Set_client));
	if (setclient == NULL)
	{
		perror("Set_client error\n");
		return -1;
	}
	setclient->wrong = 0;
	//用来发送信息给客户端
	struct sockaddr_in clientaddr;
	alen = sizeof(clientaddr);
	int maxevents;
	sfd = CreateSocket(12345);
	if (sfd < 0)
	{
		printf("create socket failled\n");
		return -1;
	}
	epfd = epoll_create(1);
	if (epfd < 0)
	{
		perror("epoll_create");
		close(sfd);
		return -1;
	}
	struct epoll_event event;
	event.events = EPOLLIN;
	event.data.fd = sfd;
	ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event);
	if (ret < 0)
	{
		perror("epoll_ctl");
		return -1;
	}
	struct epoll_event events[20];
	maxevents = 20;
	printf("员工管理系统请求链接\n");
	while (1)
	{
		//这里等待data->change输入值
		//	printf("*********************************\n");
		ret = epoll_wait(epfd, events, maxevents, -1);
		//ret为事件个数
		//i为events数组的下标
		//-1表示阻塞
		if (ret > 0)
		{
			for (i = 0; i < ret; i++)
			{
				//每个事件带有一个文件描述符
				if (sfd == events[i].data.fd)
				{
					cfd = accept(events[i].data.fd, (struct sockaddr *)&clientaddr, &alen);
					printf("###########################################\n");
					printf("		[1]登入:\n");
					printf("		[2]注册:\n");
					event.events = EPOLLIN; //关注读事件
					event.data.fd = cfd;	//添加cfd到关注事件中
					epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &event);
				}
				else
				{
					//接收到客户端发来的数据存入结构体user_data_epoll中用data接收
					len = recv(events[i].data.fd, data, sizeof(user_data), 0);
					if (len == 0)
					{
						event.events = EPOLLIN;			   //关注读事件
						event.data.fd = events[i].data.fd; //删除cfd事件
						epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &event);
					}
					//输入用户名和注册输入用户名
					//当data->key==1.2.3时都会进入这里
					if (data->key == 1 || data->key == 2 || data->key == 0)
					{
						//当输入1时login会打印请输入登入密码
						//2是要求输入注册账号和密码
						Login(data, l);
						if (0 == data->key)
						{
							//这里继续阻塞等待客户端输入
							//开启客户端的switch所以要client_key=-1;
							setclient->client_key = -1;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
							continue;
						}
						//到这说明输入正确
						//client_key = 1;
						//发送给客户端要求客户端接下来输入用户名和密码
						//这时data->key = 1或2
						if (1 == data->key)
						{
							setclient->client_key = 1;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						}
						else if (2 == data->key)
						{
							setclient->client_key = 2;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						}
					}
					else if (data->key == 1111)
					{
						fd = find(data, l);
						if (fd == -1)
						{
							//如果没找到打印登入 注册
							//发送给客户端要求客户端打印用户名错误
							//接下来输入用户名和密码
							setclient->client_key = 4;
							//发送给客户端要求客户端接下来输入用户名和密码
							//setclient->client_key = -1;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
							//client_key = -1;
							//发送给客户端让客户接下来端输入change
							continue;
						}
						//如果找到要求输入管理员密码
						setclient->client_key = 3;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 3;
																						  //发送给客户端要求客户端接下来输入管理员密码administrator
																						  //注册添加用户
					}
					else if (data->key == 2222)
					{
						if (adduser(data, l) == 0)
						{
							//注册用户成功打印登入 注册
							//发送给客户端要求客户端接下来输入data
							setclient->client_key = LOGIN_SUCCESS;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						}
						else
						{
							//注册失败
							setclient->client_key = -1;
							setclient->wrong = LOGIN_DEFEAT;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
							setclient->wrong = 0;

							continue;
						}
					}
					else if (data->key == LOGIN_SUCCESS)
					{
						adddata(data, l);
						setclient->wrong = LOGIN_SUCCESS;
						setclient->client_key = -1;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						setclient->wrong = 0;
						continue;
					}
					//管理密码错误普通用户确
					else if (data->key == 3)
					{
						common_user(data, l);
						setclient->client_key = 11;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 11
																						  //发送给客户端要求客户端接下来输入change_common选项
																						  //管理密码正确进入管理员用户
					}
					else if (data->key == 4)
					{
						setclient->client_key = 21;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						//client_key = 21
						//发送给客户端要求客户端接下来输入change_manage选项
						//管理员用户
						manage_user(data, l);
						//普通用户修改密码
					}
					else if (data->key == 11)
					{
						printf("请输入新密码\n");
						setclient->data_key = 111;
						setclient->client_key = 111;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 111
																						  //发送给客户端要求客户端接下来输入data->password
					}
					else if (data->key == 111)
					{
						if (common_change_password(data, l) == -1)
						{
							setclient->wrong = CHANGE_DEFEAT;
						}
						else
						{
							setclient->wrong = CHANGE_SUCCESS;
						}
						common_user(data, l);
						setclient->client_key = 11;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 11
																						  //发送给客户端要求客户端接下来输入change_common
																						  //普通用户查询用户
						setclient->wrong = 0;
					}
					else if (data->key == 12)
					{
						printf("请输入要查询的用户名\n");
						setclient->data_key = 122;
						setclient->client_key = 122;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 122
																						  //发送给客户端要求客户端接下来输入data->username
					}
					else if (data->key == 122)
					{
						//当失败时候
						if (-1 == find_user(data, l))
						{
							printf("没查到用户\n");
							setclient->wrong = FIND_DEFEAT;
							common_user(data, l);
							setclient->client_key = 11;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0);
							setclient->wrong = 0;
						}
						else
						{
							common_user(data, l);
							setclient->client_key = 11;
							setclient->wrong = FIND_SUCCESS;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0);
							slen = send(events[i].data.fd, find_show_buf, sizeof(find_show_buf), 0);
							setclient->wrong = 0;
						}
						//client_key = 11
						//发送给客户端要求客户端接下来输入change_common
						//退出系统
					}
					else if (data->key == 13)
					{
						event.events = EPOLLIN;			   //关注读事件
						event.data.fd = events[i].data.fd; //删除cfd事件
						epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &event);
						setclient->client_key = 255;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						exit_user(data, l, setclient);
						//break;
						//显示所有用户
					}
					else if (data->key == 14)
					{
						int events_data_fd = events[i].data.fd;
						show(data, l, &events_data_fd, setclient);
						common_user(data, l);
						//setclient->client_key有client_key=11的不用
						//输入data->key
						//setclient->client_key = 11;
						//slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						//client_key = 11
						//发送给客户端要求客户端接下来输入change_common
						//管理员添加用户
					}
					else if (data->key == 15)
					{
						printf("###########################################\n");
						printf("		[1]登入:\n");
						printf("		[2]注册:\n");
						setclient->client_key = -1;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0);
					}
					else if (data->key == 21)
					{
						printf("请输入添加者的用户名\n");
						printf("请输入添加者的密码\n");
						printf("请输入添加者的电话:\n");
						printf("请输入添加者的职位:\n");
						printf("请输入添加者的等级:\n");
						printf("请输入添加者的工资:\n");
						printf("请输入添加者的年龄:\n");
						//发送给客户端接下来输入用户名和密码
						setclient->client_key = 211;
						setclient->data_key = 211;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
					}
					else if (data->key == 211)
					{
						if (-1 == manage_adduser(data, l))
						{
							setclient->wrong = ADD_DEFEAT;
						}
						else
						{
							setclient->wrong = ADD_SUCCESS;
						}
						manage_user(data, l);
						setclient->client_key = 21;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 21
																						  //发送给客户端要求客户端接下来输入change_manage选项
																						  //管理员删除用户
						setclient->wrong = 0;
					}
					else if (data->key == 22)
					{
						printf("请输入要删除的用户名称\n");
						setclient->data_key = 222;
						setclient->client_key = 222;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 222
																						  //发送给客户端要求客户端接下来输入data->username
																						  //管理员删除用户
					}
					else if (data->key == 222)
					{
						if (-1 == manage_delete(data, l))
						{
							setclient->wrong = DELETE_DEFEAT;
						}
						else
						{
							setclient->wrong = DELETE_SUCCESS;
						}
						manage_user(data, l);
						setclient->client_key = 21;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 21
																						  //发送给客户端要求客户端接下来输入change_manage选项
						setclient->wrong = 0;
					}
					else if (data->key == 23)
					{
						printf("请输入需要修改者的用户名\n");
						printf("请输入新密码\n");
						printf("请输入新的电话:\n");
						printf("请输入新的职位:\n");
						printf("请输入新的等级:\n");
						printf("请输入新的工资:\n");
						printf("请输入新的年龄:\n");
						setclient->data_key = 233;
						setclient->client_key = 233;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //data->key == 233;//让客户端的data->key也变为233
																						  //client_key = 233
																						  //发送给客户端要求客户端接下来输入data->username和data->password
																						  //管理员修改用户
					}
					else if (data->key == 233)
					{
						if (-1 == manage_change(data, l))
						{
							setclient->wrong = CHANGE_DEFEAT;
						}
						else
						{
							setclient->wrong = CHANGE_SUCCESS;
						}
						manage_user(data, l);
						setclient->client_key = 21;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 21
																						  //发送给客户端要求客户端接下来输入change_manage选项
																						  //管理员用户查询用户
						setclient->wrong = 0;
					}
					else if (data->key == 24)
					{
						printf("请输入要查询的用户名\n");
						setclient->data_key == 244;
						setclient->client_key = 244;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 244
																						  //发送给客户端要求客户端接下来输入data->username
					}
					else if (data->key == 244)
					{
						if (-1 == find_user(data, l))
						{
							printf("没查到用户\n");
							setclient->wrong = FIND_DEFEAT;
							manage_user(data, l);
							setclient->client_key = 21;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
							setclient->wrong = 0;
						}
						else
						{
							setclient->wrong = FIND_SUCCESS;
							setclient->client_key = 21;
							slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
							slen = send(events[i].data.fd, find_show_buf, sizeof(find_show_buf), 0);
							manage_user(data, l);
							setclient->wrong = 0;
						}
					}
					else if (data->key == 25)
					{
						event.events = EPOLLIN;			   //关注读事件
						event.data.fd = events[i].data.fd; //删除cfd事件
						epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &event);
						setclient->client_key = 255;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
						exit_user(data, l, setclient);
						//break;
					}
					else if (data->key == 26)
					{
						int events_data_fd = events[i].data.fd;
						show(data, l, &events_data_fd, setclient);
						manage_user(data, l);
						setclient->client_key = 21;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0); //客户端用switch做相应处理
																						  //client_key = 21
																						  //发送给客户端要求客户端接下来输入change_manage选项
					}
					else if (data->key == 27)
					{
						printf("###########################################\n");
						printf("		[1]登入:\n");
						printf("		[2]注册:\n");
						setclient->client_key = -1;
						slen = send(events[i].data.fd, setclient, sizeof(Set_client), 0);
						break;
					}
				}
			}
		}
	}
	return 0;
}
//登录判断处理
void Login(user_data *data, user *l)
{
	if (data->key == 1)
	{
		printf("###########################################\n");
		printf("       	 请输入用户名:\n");
		printf("     	 请输入用户密码:\n");
	}
	else if (data->key == 2)
	{
		printf("###########################################\n");
		printf("   		 请输入要注册的用户名:\n");
		printf("   		 请输入要注册用户密码:\n");
	}
	else
	{
		printf("###########################################\n");
		printf("   		 输入有误请重新输入\n");
		printf("\n");
		printf("###########################################\n");
		printf("		[1]登入:\n");
		printf("		[2]注册:\n");
	}
}
//添加注册的用户信息
int adddata(user_data *data, user *l)
{
	user *temp = NULL;
	temp = (user *)malloc(sizeof(user));
	if (temp == NULL)
	{
		printf("malloc temp error\n");
		return -1;
	}
	printf("		添加成功\n");
	printf("###########################################\n");
	printf("		请输入电话:\n");
	printf("		请输入职位:\n");
	printf("		请输入等级:\n");
	printf("		请输入工资:\n");
	printf("		请输入年龄:\n");
	strcpy(temp->username, data->username);
	strcpy(temp->password, data->password);
	strcpy(temp->phone, data->phone);
	strcpy(temp->work, data->work);
	temp->level = data->level;
	temp->salary = data->salary;
	temp->age = data->age;
	temp->next = l->next;
	l->next = temp;
	//链表数据传输到文件中
	linklist_to_txt(l);
	printf("###########################################\n");
	printf("		[1]登入:\n");
	printf("		[2]注册:\n");
	return 0;
}

//客户端信息链表的添加,失败返回-1
int adduser(user_data *data, user *l)
{
	while (l->next != NULL)
	{
		if ((strcmp(l->next->username, data->username) == 0))
		{
			printf("###########################################\n");
			printf("用户名重复请重新注册");
			return -1;
		}
		l = l->next;
	}
	return 0;
}
//客户段信息查找匹配用户密码,失败返回-1
int find(user_data *data, user *l)
{
	if (l->next == NULL)
	{
		printf("用户名或密码错误请重新登入\n");
		printf("###########################################\n");
		printf("		[1]登入:\n");
		printf("		[2]注册:\n");
		return -1;
	}
	while (l->next != NULL)
	{
		if ((strcmp(l->next->username, data->username) == 0) &&
			(strcmp(l->next->password, data->password) == 0))
		{
			printf("登入成功\n");
			printf("请输入管理员密码\n");
			//		a[4] = 4;
			return 0;
		}
		l = l->next;
	}
	printf("登入失败请重新登入\n");
	printf("###########################################\n");
	printf("		[1]登入:\n");
	printf("		[2]注册:\n");
	return -1;
}
//普通用户权限处理事件
int common_user(user_data *data, user *l)
{
	printf("###########################################\n");
	printf("[1]修改密码\n");
	printf("[2]查询用户\n");
	printf("[3]退出系统\n");
	printf("[4]显示所有用户\n");
	printf("[5]返回上一级\n");
}
//管理用户权限处理事件
int manage_user(user_data *data, user *l)
{
	printf("###########################################\n");
	printf("[1]增加用户\n");
	printf("[2]删除用户\n");
	printf("[3]修改用户\n");
	printf("[4]查询用户\n");
	printf("[5]退出系统\n");
	printf("[6]显示所有用户\n");
	printf("[7]返回上一级\n");
}
//普通用户修改密码
int common_change_password(user_data *data, user *l)
{
	while (l->next != NULL)
	{
		if ((strcmp(l->next->username, data->username) == 0))
		{
			strcpy(l->next->password, data->password);
			printf("修改密码成功\n");
			//链表数据传输到文件中
			linklist_to_txt(l);
			return 0;
		}
		l = l->next;
	}
	return -1;
}
//判断链表是否为空
int empty(user *l)
{
	if (l->next == NULL)
	{
		printf("数据库为空\n");
		return -1;
	}
	return 0;
}
//查询用户
int find_user(user_data *data, user *l)
{
	memset(find_show_buf, 0, sizeof(find_show_buf));
	while (l->next != NULL)
	{
		if (strcmp(l->next->username, data->username) == 0)
		{
			sprintf(find_show_buf, "用户名:%s	电话：%s	职位：%s	等级：%d	工资：%d	年龄：%d", l->next->username, l->next->phone,
					l->next->work, l->next->level, l->next->salary, l->next->age);
			printf("%s\n", find_show_buf);
			return 0;
		}
		l = l->next;
	}
	return -1;
}
//显示所有用户
int show(user_data *data, user *l, int *cfd, Set_client *setclient)
{
	memset(find_show_buf, 0, sizeof(find_show_buf));
	int slen;
	setclient->wrong = SHOW_SUCCESS;
	if (data->key == 14)
	{
		setclient->client_key = 11;
	}
	else
	{
		setclient->client_key = 21;
	}
	while (l->next != NULL)
	{
		sprintf(find_show_buf + strlen(find_show_buf), "用户名:%s     电话:%s     职位:%s     等级:%d     工资:%d     年龄:%d\n", l->next->username, l->next->phone,
				l->next->work, l->next->level, l->next->salary, l->next->age);
		l = l->next;
	}
	slen = send(*cfd, setclient, sizeof(Set_client), 0);
	slen = send(*cfd, find_show_buf, sizeof(find_show_buf), 0);
	printf("%s", find_show_buf);
	setclient->wrong = 0;
	return 0;
}
//退出系统
int exit_user(user_data *data, user *l, Set_client *setclient)
{
	//链表数据传输到文件中
	linklist_to_txt(l);
	printf("退出成功\n");
}
//管理员添加用户
int manage_adduser(user_data *data, user *l)
{
	user *temp = NULL;
	temp = (user *)malloc(sizeof(user));
	if (temp == NULL)
	{
		printf("malloc temp error\n");
		return -1;
	}
	while (l->next != NULL)
	{
		if (strcmp(l->next->username, data->username) == 0)
		{
			return -1;
		}
		l = l->next;
	}
	strcpy(temp->username, data->username);
	strcpy(temp->password, data->password);
	strcpy(temp->phone, data->phone);
	strcpy(temp->work, data->work);
	temp->level = data->level;
	temp->salary = data->salary;
	temp->age = data->age;
	temp->next = l->next;
	l->next = temp;
	//链表数据传输到文件中
	linklist_to_txt(l);
	printf("添加成功\n");
	return 0;
}
//管理员删除用户
int manage_delete(user_data *data, user *l)
{
	int a = empty(l);
	if (a == -1)
	{
		return -1;
	}
	user *p;
	while (l->next != NULL)
	{
		if ((strcmp(l->next->username, data->username) == 0))
		{
			p = l->next;
			l->next = p->next;
			free(p);
			printf("删除用户成功\n");
			//链表数据传输到文件中
			linklist_to_txt(l);
			return 0;
		}
		l = l->next;
	}
	printf("没有查询到要删除的用户\n");
	return -1;
}
//管理员修改用户
int manage_change(user_data *data, user *l)
{
	while (l->next != NULL)
	{
		if (strcmp(l->next->username, data->username) == 0)
		{
			strcpy(l->next->password, data->password);
			strcpy(l->next->phone, data->phone);
			strcpy(l->next->work, data->work);
			l->next->level = data->level;
			l->next->salary = data->salary;
			l->next->age = data->age;
			printf("修改成功\n");
			//链表数据传输到文件中
			linklist_to_txt(l);
			return 0;
		}
		l = l->next;
	}
	printf("没查到该用户\n");
	return -1;
}
//数据从文件放在链表里面
int txt_to_linklist(user *l)
{
	FILE *fp = fopen("data.txt", "r");
	if (!fp)
	{
		perror("fopen");
		return -1;
	}
	user *tmp = l;
	user *exchang = l;
	char username[40]; //用户名
	char password[30]; //密码
	char phone[30];	//电话
	char work[30];	 //职位
	int level;		   // 等级
	int salary;		   // 工资
	int age;		   // 年龄
	while (fscanf(fp, " %s %s %s %s %d %d %d", username, password,
				  phone, work, &level, &salary, &age) != EOF)
	{
		tmp = (user *)malloc(sizeof(user));
		tmp->level = level;
		tmp->salary = salary;
		tmp->age = age;
		strcpy(tmp->username, username);
		strcpy(tmp->password, password);
		strcpy(tmp->phone, phone);
		strcpy(tmp->work, work);
		exchang->next = tmp;
		exchang = tmp;
	}
	exchang->next = NULL;
	fclose(fp);
	return 0;
}
//链表数据传输到文件中
int linklist_to_txt(user *l)
{
	FILE *fp = fopen("data.txt", "w+");
	if (!fp)
	{
		perror("fopen");
		return -1;
	}
	user *tmp1 = l->next;
	while (tmp1 != NULL)
	{
		fprintf(fp, " %s %s %s %s %d %d %d", tmp1->username, tmp1->password,
				tmp1->phone, tmp1->work, tmp1->level, tmp1->salary, tmp1->age);
		tmp1 = tmp1->next;
	}
	fclose(fp);
	return 0;
}
